/**
 * 北京中流科技有限公司 版权所有
 * http://www.chinasoa.com.cn
 */
package cn.com.chinasoa.course.subscribe;

import java.io.IOException;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import javax.persistence.EntityManager;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletException;

import cn.com.chinasoa.course.subscribe.model.ArrangedCourse;
import cn.com.chinasoa.course.subscribe.model.PrivilegeStrategy;
import cn.com.chinasoa.course.subscribe.model.TrainingAddress;
import cn.com.chinasoa.course.subscribe.model.TrainingSubject;
import cn.com.chinasoa.course.subscribe.util.DataTypeUtil;
import cn.com.chinasoa.course.subscribe.util.EntityManagerHelper;

import com.x4ee.soa.portal.framework.core.ActionConfig;
import com.x4ee.soa.portal.framework.core.ActionFilter;
import com.x4ee.soa.portal.framework.core.BaseRequestFilter;

/**
 * @author 刘洋(liuyang@chinasoa.com.cn)
 * 2007-11-1上午02:08:00
 */
public class ArrangedCourseCreateActionFilter extends BaseRequestFilter
		implements ActionFilter {

	private static String SUCCESS = "success";
	private static String FAILED = "error";

	public String process(ActionRequest actionRequest, ActionResponse actionResponse,
			ActionConfig actionConfig) throws PortletException, IOException {

		boolean success = false;

		String courseTitle = actionRequest.getParameter("courseTitle");
		String courseDescription = actionRequest.getParameter("courseDescription");
		String trainingSubject = actionRequest.getParameter("trainingSubject");
		
		Calendar startDate = DataTypeUtil.parseCalender(actionRequest.getParameter("startDate"));
		Calendar endDate = DataTypeUtil.parseCalender(actionRequest.getParameter("endDate"));
		
		String trainingAddress = actionRequest.getParameter("trainingAddress");

		List<Long> privilegeStrategyIdList = DataTypeUtil.getParamLongList("privilegeStrategy_",actionRequest);
		
		if((courseTitle!=null)&&(courseDescription!=null)&&(trainingSubject!=null)&&(startDate!=null)&&
				(endDate!=null)&&(trainingAddress!=null)){

			EntityManager em = EntityManagerHelper.getEntityManager();
			em.getTransaction().begin();
			ArrangedCourse vArrangedCourse = new ArrangedCourse();
			vArrangedCourse.setCourseTitle(courseTitle);
			vArrangedCourse.setCourseDescription(courseDescription);

			TrainingSubject tTrainingSubject = em.find(TrainingSubject.class, DataTypeUtil.parseLong(trainingSubject));
			vArrangedCourse.setTrainingSubject(tTrainingSubject);
			
			TrainingAddress tTrainingAddress = em.find(TrainingAddress.class, DataTypeUtil.parseLong(trainingAddress));
			vArrangedCourse.setTrainingAddress(tTrainingAddress);

			for(Iterator<Long> psIt = privilegeStrategyIdList.iterator();psIt.hasNext();){
				PrivilegeStrategy tPrivilegeStrategy = em.find(PrivilegeStrategy.class,psIt.next());
				vArrangedCourse.getPrivilegeStrategys().add(tPrivilegeStrategy);
			}

			vArrangedCourse.setStartDate(startDate);
			vArrangedCourse.setEndDate(endDate);
			
			em.persist(vArrangedCourse);
			em.getTransaction().commit();
			EntityManagerHelper.closeEntityManager();
			
			success = true;

		}else{
			success = false;
		}

		if(success)return SUCCESS;
		else return FAILED;
	}

}
